using System;
using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace SoftwareMonkeys.FileNodes
{
	/// <summary>
	/// 
	/// </summary>
	public class FileNodeSaver
	{
		public FileNodeSaver()
		{
		}
		
		public void Save(FileNode node, string filePath)
		{
			PrepareSave(node);
			
			string dir = Path.GetDirectoryName(filePath);
			
			if (!Directory.Exists(dir))
				Directory.CreateDirectory(dir);
			
			string str = JsonConvert.SerializeObject(node, Formatting.Indented);
			
			using (StreamWriter writer = File.CreateText(filePath))
			{
				writer.Write(str);
			}
		}
		
		public void PrepareSave(FileNode node)
		{
			if (node.Properties.ContainsKey("Name"))
			{
				node.Name = node.Properties["Name"];
				node.Properties.Remove("Name");
			}
		}
	}
}
